package com.example.service;

import com.example.dao.UserDao;
import com.example.entity.User;
import com.example.entity.menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

      private UserDao userDao;

      @Autowired
      public UserServiceImpl(UserDao userDao) {
            this.userDao = userDao;
      }

      @Override
      public User login(String userName, String userPwd) {
//            通过用户名判断用户是否存在
            User findByName=userDao.findByName(userName);
            if (ObjectUtils.isEmpty(findByName))throw new RuntimeException("用户不存在");
//          比较密码
            if (!findByName.getUserPwd().equals(userPwd))  throw new RuntimeException("密码错误");

            return findByName;

      }

      @Override
      public void register(User user) {
            userDao.register(user);
      }

      @Override
      public User findUserByID(Integer randomID) {
            return userDao.findUserByID(randomID);
      }

      @Override
      public List<menu> findAllMenu() {
            return userDao.findAllMenu();
      }

      @Override
      public User findUserDetailByID(Integer id) {
           return userDao.findUserDetailByID(id);
      }

      @Override
      public void updateUserDetail(User user) {
            userDao.updateUserDetail(user);
      }
}
